% The code is based on the following paper:
%   Chen, T. Y., Lin, Y. L., and Tzeng, L. Y., forthcoming.
%   Estimating probability weighting functions through option pricing bounds. Review of Asset Pricing Studies.
%
% Copyright: Tzu-Ying Chen, Yo-Lan Lin, Larry Y. Tzeng
% Date: January 30, 2024

clear

%% Setting
BV_Target = {'UB'};                                                       

%% An Example: Option Quotes and Option Pricing Bounds
Target_Date = 20201021;                                                    % Given
Target_PWF = [0.72 0.80];                                                  % Given

% Load Data
FileName = ['Summary_OP_Grid_' num2str(Target_Date)];
load(FileName, ...
     'OP_Bid', ...
     'K', 'KS', 'CPFlag', 'S0', 'S0_ADJ', 'TTM', 'RF', 'DY', ...
     'TC', ...
     'State_Monthly', 'State_PROB');
clear FileName

Type = cell(size(K));                                                      
Type(CPFlag==1) = {'Call'};   
Type(CPFlag==2) = {'Put'};    

% Expected Utility
OP_UB_EU = RDEU_OP_UB(State_Monthly, State_PROB, ...
                      K, CPFlag, TTM, S0, S0_ADJ, RF, DY, TC, ...
                      1, 1);   
OP_UB_EU = OP_UB_EU';                                                      

VIORate = sum(OP_UB_EU < OP_Bid) / length(OP_Bid);

% Rank-Dependent Expected Utility                   
OP_UB_RDEU = RDEU_OP_UB(State_Monthly, State_PROB, ...
                        K, CPFlag, TTM, S0, S0_ADJ, RF, DY, TC, ...
                        Target_PWF(1), Target_PWF(2));   
OP_UB_RDEU = OP_UB_RDEU';      

clear K CPFlag S0 S0_ADJ TTM RF DY TC
clear State_Monthly State_PROB
clear Type

%% Plot Figure: Upper Bound on the Specific Date 
AllPeriod = Target_Date;
AllPeriod_Text = cellstr(char(['\qquad \qquad \qquad \qquad ' datestr(datenum(num2str(AllPeriod), 'yyyymmdd'), 'mmm dd, yyyy')]));

% Plot Figure
h_UB = figure;
plot(KS, OP_UB_RDEU, ...
     'LineStyle', '-', ...
     'LineWidth', 2, ...
     'Marker', '.', ...
     'MarkerSize', 25, ...     
     'Color', 'r')
hold on

plot(KS, OP_UB_EU, ...
     'LineStyle', ':', ...
     'LineWidth', 2, ...
     'Marker', '.', ...
     'MarkerSize', 25, ...          
     'Color', 'b')
hold on

plot(KS, OP_Bid, ...
     'LineStyle', 'None', ...
     'Marker', 'o', ...
     'MarkerSize', 7, ...
     'Color', 'k')
hold on
grid on

h = legend(['($\alpha$ , $\beta$) = (' num2str(Target_PWF(1)) ' , ' num2str(Target_PWF(2)) ')'], ...
           ['($\alpha$ , $\beta$) = (' num2str(1) ' , ' num2str(1) ')'], ...
           'Bid Price');
set(h, 'FontSize', 15, ...
       'FontName', 'Times New Roman', ...
       'FontWeight', 'Bold', ...
       'Location', 'NorthEast', ...
       'Interpreter', 'Latex', ...
       'Box', 'Off')
clear h
        
h1 = xlabel('Moneyness');  
h2 = ylabel('Option Prices');   
h3 = title([AllPeriod_Text; '(percentage of violations under EU = ' num2str(roundn(100 * VIORate, - 2)) '\%)']);
set([h1 h2 h3], 'FontSize', 15, ...
                'FontName', 'Times New Roman', ...
                'FontWeight', 'Bold', ...
                'Interpreter', 'Latex')
clear h1 h2 h3       

set(gca, 'XTick', 0.9:0.01:1.05, ...
         'XLim', [0.9 1.05], ...
         'YTick', 0:50:500, ...
         'YLim', [0 400], ...
         'FontSize', 15, ...
         'FontName', 'Times New Roman')    

set(gca, 'Layer', 'Top')          

X = 29.7;                                                                  
Y = 21.0;                                                                  
XMargin = 1;                                                               
YMargin = 1;                                                               
XSize = X - 2 * XMargin;                                                   
YSize = Y - 2 * YMargin;                                                   
set(h_UB, 'PaperUnits', 'Centimeters')
set(h_UB, 'PaperSize', [X Y])
set(h_UB, 'PaperPosition', [XMargin YMargin XSize YSize])
set(h_UB, 'PaperOrientation', 'Portrait')
clear AllPeriod AllPeriod_Text h_UB X Y XMargin YMargin XSize YSize 
clear KS OP_Bid OP_UB_EU OP_UB_RDEU VIORate

%% Output
FileName = ['Figure_Example_UB'];
saveas(gcf, [FileName '.eps'], 'epsc')
print(FileName, '-dpdf', '-fillpage')
winopen([FileName '.pdf'])
clear FileName
